home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
admin
/
linuxcon.000
/
linuxcon
/
linuxconf-1.6
/
userconf
/
perm.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-24
|
6KB
|
206 lines
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
#include "../misc/misc.h"
#include "../netconf/netconf.h"
#include "userconf.h"
#include "internal.h"
#include "userconf.m"
static time_t lasttime = 0;
/*
Force a one minute "no question asked" root autorization for
one minute.
Normally linuxconf does nothing priviledged unless the user
has provided some password (generally root). Some operation
are indeed priviledge but are accepted, for example, changing
his/her password.
*/
void perm_forceok()
{
lasttime = time(&lasttime);
}
#define VALIDATION_TIME (2*60)
/*
Get the crypt password of a user
*/
static int perm_getupass (const char *username, char password[])
{
/* #Specification: password / strategy / by hand
To support transparently standard and shadow password without
recompiling, linuxconf read manually the /etc/passwd and
/etc/shadow files.
This is causing problem for NIS user though. This will
have to be fixed.
*/
USERS users;
USER *usr = users.getitem(username);
int ret = -1;
if (usr != NULL){
SHADOW *shadow = users.getshadow(usr);
const char *pwd = usr->getpwd();
if (shadow != NULL){
pwd = shadow->getpwd();
}
strcpy (password,pwd);
ret = 0;
}
return ret;
}
/*
Sent by the html mode to identify the user.
Currently, only root is accepted as a password.
Ultimatly, www administrator could be defined.
*/
void perm_setaccess (const char *username, const char *password)
{
time_t curtime = time(&curtime);
lasttime = curtime - (VALIDATION_TIME + 1); // Make last autorization
// obsolete
if (strcmp(username,"root")==0){
if (simul_isdemo()){
/* #SpΘcification: linuxconf / demo mode / root password
The root password in demo mode is always linux.
*/
if (strcmp(password,"linux")==0){
lasttime = curtime;
}
}else{
char upass[100];
if (perm_getupass(username,upass)!=-1){
if (upass[0] != '\0'){
if (strcmp(crypt(password, upass),upass)==0){
lasttime = curtime;
}
}else if (password[0] == '\0'){
lasttime = curtime;
}
}
}
}
}
/*
Verify if there is password for root. If so ask the user
to enter it and validate it. So askrunlevel is "safe". No
one will be allowed to reconfigured the network if he don't
know the root password.
Return != 0 if user if allowed to get in.
*/
int perm_checkpass ()
{
time_t curtime = time(&curtime);
/* #Specification: root access / timeout
When the user select a configuration task, the password for root
must be supplied. This "validation" is good for 2 minute.
It means that the user may do several configuration in one
minutes without being asked for the root password every time.
If the user wait a minute or more, the password will be
asked again. Look safe and user friendly to me.
*/
int ret = 1;
if (curtime - lasttime > VALIDATION_TIME){
char upass[100];
if (perm_getupass ("root",upass) == -1){
xconf_error (MSG_U(E_NOROOT
,"No root user in /etc/passwd\n"
"Better to let you in"));
}else if (upass[0] != '\0'){
/* #Specification: root access / password validation
When the admin/user must provide the root
password, he has 3 chances.
*/
if (dialog_mode == DIALOG_HTML){
html_needpasswd();
ret = 0;
}else{
for (int i=0; i<3; i++){
char passstr[MAX_LEN+1];
if (xconf_inputpass (MSG_U(T_PASSREQ,"Password requiered")
,MSG_U(I_ENTERPASS
,"Enter password for root\n"
"Only the superuser is allowed to perform\n"
"configuration task.")
,help_nil
,passstr) != MENU_ACCEPT){
ret = 0;
break;
}else if (strcmp(crypt(passstr, upass),upass)!=0){
ret = 0;
xconf_error (MSG_U(E_IVLDPASS,"Invalid password"));
}else{
ret = 1;
break;
}
}
}
}
}
if (ret) lasttime = curtime;
return ret;
}
static int perm_html_mode = 0;
/*
Check if the user is really root. If it is not, but the effective ID
is root, then ask for the root password.
Return != 0 if the real UID is root or the user knows the root password.
It prints an informative message about the action which will occur.
*/
int perm_rootaccess(const char *ctl, ...)
{
/* #Specification: configurator / setuid root
The configurator (anything-conf) can be set setuid root.
It will allows normal users to get in and then will ask
for the root password at the proper time. This strategy
make the system friendlier. It allows normal user to
inspect the configuration (if allowed) and when finding
something odd, use the root password (if known) to fix
things, The idea here is that we generally think first
about getting somewhere and later about the permissions
needed to get there.
The nice thing about this scheme is that this program
will deny root access automaticly after some time.
If you don't like this, then don't set it setuid root. It
will operate correctly and won't bug you with password.
*/
int ret = perm_html_mode ? 0 : getuid()==0;
if (geteuid()==0){
if (!ret){
ret = perm_checkpass();
}
}else{
va_list list;
va_start (list,ctl);
char buf[1000];
vsprintf (buf,ctl,list);
va_end (list);
xconf_error (MSG_U(E_MUSTBEROOT,"You must be root to\n%s"),buf);
}
return ret;
}
/*
Set the html mode for granting priviledge.
In html mode we are executing as root all the time. Normally
no question is asked to root. Now we must ask for password before
going through.
*/
void perm_sethtml (int _mode)
{
perm_html_mode = _mode;
}